package com.haha;

import org.json.JSONObject;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

public class ConfigManager {
    private static final String CONFIG_PATH = System.getProperty("user.home") + File.separator + "wowHelperKeybinds.json";
    private static final Map<String, Map<String, String>> keyBindings = new HashMap<>();

    static {
        loadConfig();
    }

    public static void loadConfig() {
        System.out.println("读取的路径为：" + CONFIG_PATH);
        try (InputStream is = new FileInputStream(CONFIG_PATH)) {
            BufferedReader reader = new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8));
            StringBuilder sb = new StringBuilder();
            String line;
            while ((line = reader.readLine()) != null) {
                System.out.println("line===" + line);
                sb.append(line);
            }

            JSONObject config = new JSONObject(sb.toString());
            JSONObject binds = config.getJSONObject("keyBinds");

            for (String profession : binds.keySet()) {
                JSONObject professionBinds = binds.getJSONObject(profession);
                Map<String, String> skills = new HashMap<>();
                for (String skill : professionBinds.keySet()) {
                    skills.put(skill, professionBinds.getString(skill));
                }
                keyBindings.put(profession, skills);
            }
        } catch (Exception e) {
            System.out.println("初始化配置...");
            initDefaultConfig();
        }
    }

    public static void saveConfig() {
        System.out.println("保存路径为：" + CONFIG_PATH);
        JSONObject config = new JSONObject();
        JSONObject binds = new JSONObject();

        for (String profession : keyBindings.keySet()) {
            JSONObject professionBinds = new JSONObject();
            keyBindings.get(profession).forEach(professionBinds::put);
            binds.put(profession, professionBinds);
        }

        config.put("keyBinds", binds);

        try (OutputStreamWriter writer = new OutputStreamWriter(
                new FileOutputStream(CONFIG_PATH), StandardCharsets.UTF_8)) {
            writer.write(config.toString(2));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static String getKeyBind(String profession, String skillName) {
        return keyBindings.getOrDefault(profession, new HashMap<>())
                .getOrDefault(skillName, "");
    }

    public static void setKeyBind(String profession, String skillName, String key) {
        keyBindings.computeIfAbsent(profession, k -> new HashMap<>())
                .put(skillName, key);
    }

    private static void initDefaultConfig() {
        // 德鲁伊默认配置
        Map<String, String> druid = new HashMap<>();
        druid.put("野蛮咆哮", "F1");
        druid.put("裂伤(豹)", "F2");
        druid.put("横扫", "F3");
        druid.put("斜掠", "F3");
        druid.put("割裂", "F3");
        druid.put("撕碎", "F3");
        druid.put("精灵之火（野性）", "F3");
        keyBindings.put("德鲁伊-野德-WLK", druid);

        // 战士默认配置
        Map<String, String> warrior = new HashMap<>();
        warrior.put("英勇打击", "F4");
        warrior.put("顺劈斩", "F5");
        warrior.put("嗜血", "F6");
        keyBindings.put("战士-狂暴-WLK", warrior);

        saveConfig();
    }
}
